/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!
  \file			edopengl.cc
  \date			Jan 2013
  \author		TNick

  \brief		Contains the implementation of EdOpenGl class


*//*


 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Please read COPYING and README files in root folder
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

#include	<QDialog>
#include	<QPlainTextEdit>
#include	<QDialogButtonBox>

#include	<cpg/cpg.h>

#include	"edopengl.h"

#include	<cpg/logic/cpgdoc.h>

#include	<cpg/actions/find/findwidget.h>

/*  INCLUDES    ============================================================ */
//
//
//
//
/*  DEFINITIONS    --------------------------------------------------------- */

using namespace cpg;
using namespace cpg::Gui;

/*  DEFINITIONS    ========================================================= */
//
//
//
//
/*  DATA    ---------------------------------------------------------------- */

/*  DATA    ================================================================ */
//
//
//
//
/*  CLASS    --------------------------------------------------------------- */

/* ------------------------------------------------------------------------- */
EdOpenGl::EdOpenGl	( QWidget * parent )
	: QWidget( parent )
{
	PGDBG2( PGDBG_CTORDTOR, "Creating EdOpenGl", this );

	setDocInternal( new CpgDoc( this ) );

	/* setup our layout for this widget */
	setLayout( &lay_ );
	lay_.addWidget( &wv_ );
	find_ = NULL;

	wv_.setContextMenuPolicy( Qt::CustomContextMenu );

	connect( &wv_, SIGNAL(customContextMenuRequested(QPoint)),
			 this, SIGNAL( contextMenu(QPoint) ) );

	connect( document(), SIGNAL( contentModified() ),
			 SLOT( contentEdited() ) );
	connect( document(), SIGNAL( fileNameModified(const QString &) ),
			 SLOT( fileNameModified(const QString &) ) );

	fileNameModified( document()->documentName() );

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
EdOpenGl::~EdOpenGl	( void )
{
	PGDBG2( PGDBG_CTORDTOR, "Destroyng EdOpenGl", this );
	/* stub */
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void			EdOpenGl::insertHtml		( const QString & content )
{
	if ( content.length() == 0 )
	{

		QDialog * d = new QDialog( this );
		d->setWindowTitle( "Insert HTML text" );
		QVBoxLayout * lay = new QVBoxLayout( d );
		QPlainTextEdit * txe = new QPlainTextEdit( d );
		/*txe->setPlainText( html_hist_ );*/
		lay->addWidget( txe );
		QDialogButtonBox * bbx = new QDialogButtonBox( d );
		bbx->setStandardButtons(
					QDialogButtonBox::Ok |
					QDialogButtonBox::Cancel );
		lay->addWidget( bbx );
		d->setLayout( lay );
		connect( bbx, SIGNAL(accepted()), d, SLOT( accept()) );
		connect( bbx, SIGNAL(rejected()), d, SLOT( reject()) );
		if ( d->exec() == QDialog::Accepted )
		{
			document()->insertHtml( txe->toPlainText() );
		}
	}
	else
	{
		document()->insertHtml( content );
	}
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void			EdOpenGl::contentEdited		( void )
{
	if ( isWindowModified() != document()->isModified() )
	{
		setWindowTitle( tr( "%1[*]" ).arg( document()->documentName() ) );
		setWindowModified( document()->isModified() );
	}
	emit contentChanged();
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void			EdOpenGl::fileNameModified		( const QString & new_name )
{

	setWindowTitle( tr( "%1[*]" ).arg( new_name ) );
	setWindowModified( document()->isModified() );
	emit contentChanged();

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void			EdOpenGl::internalLinkReq		( const QUrl & req_url )
{
	/** @todo internalLinkReq */
	Q_UNUSED( req_url );
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void			EdOpenGl::zoomIn				( qreal amount )
{
	/*wv_.setZoomFactor( wv_.zoomFactor() + amount );*/
	 /** @todo implement amount */
	Q_UNUSED( amount );
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void			EdOpenGl::zoomOut				( qreal amount )
{
	/*qreal value = wv_.zoomFactor() - amount;
	if ( value < 0.010 )
		value = 0.010;
	wv_.setZoomFactor( value );*/
	 /** @todo implement zoomOut */
	Q_UNUSED( amount );
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void			EdOpenGl::setZoom				( qreal value )
{
	/*if ( value < 0.010 )
		value = 0.010;
	else if ( value > 20.0 )
		value = 20.0;
	wv_.setZoomFactor( value );*/
	Q_UNUSED( value );
	 /** @todo implement setZoom */
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void			EdOpenGl::goToAnchor			( const QString & s_name )
{
	/** @todo goToAnchor */
	Q_UNUSED( s_name );

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void			EdOpenGl::showFind				( bool b_visible )
{
	QString s_find_tx = selectedText() ;
	if ( find_ == NULL )
	{
		if ( b_visible == false )
		{
			return;
		}
		find_ = new FindWidget(
					s_find_tx, false,
					( document()->isReadOnly() == false ),
					this );
		lay_.addWidget( find_ );

		connect( find_, SIGNAL( closeReq() ), this, SLOT( showFind() ) );
		connect( find_, SIGNAL( findNext(const QString &) ),
				 this, SLOT( findNext(const QString &) ) );
		connect( find_, SIGNAL( findPrev(const QString &) ),
				 this, SLOT( findPrev(const QString &) ) );
		connect( find_, SIGNAL( replaceAll(const QString &,const QString &) ),
				 this, SLOT( replaceAll(const QString &,const QString &) ) );
		connect( find_, SIGNAL( replaceNext(const QString &,const QString &) ),
				 this, SLOT( replaceNext(const QString &,const QString &) ) );
		connect( find_, SIGNAL( replaceOne(const QString &,const QString &) ),
				 this, SLOT( replaceOne(const QString &,const QString &) ) );

		emit findVisibilityChanged( true );
		findNext( s_find_tx );
	}
	else
	{
		if ( b_visible == true )
		{
			find_->setFindText( s_find_tx );
			findNext( s_find_tx );
			return;
		}
		else
		{
			lay_.removeWidget( find_ );
			find_->deleteLater();
			find_ = NULL;
			emit findVisibilityChanged( false );
		}
	}
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
QString			EdOpenGl::selectedText				( void )
{

	/** @todo selectedText */
	return QString();

}
/* ========================================================================= */




/*  CLASS    =============================================================== */
//
//
//
//
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
